package com.lysj.bhp.financial.controller;

import com.lysj.bhp.financial.base.FinancialContent;
import com.lysj.bhp.financial.domain.Financial;
import com.lysj.bhp.financial.domain.UserFinancialRecord;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("admin/financial")
public class FinancialController extends FinancialContent {

    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @GetMapping("list")
    public Resp list(Financial model) {
        model.setIsExist(1);
        Specification specification = ParamUtil.createSpecification(model);
        List<Financial> financialList = financialRepository.findAll(specification, new Sort(Sort.Direction.DESC, "createDate"));
        return new Resp(financialList);
    }

    @PostMapping("add")
    public Resp add(Financial model) {
        if (ParamUtil.isBlack(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "名称不能为空");
        }
        if (ParamUtil.isBlack(model.getEnglishName())) {
            return new Resp(Resp.PARAM_ERROR, "英文名称不能为空");
        }
        if (ParamUtil.isBlack(model.getTitle())) {
            return new Resp(Resp.PARAM_ERROR, "标题不能为空");
        }
        if (ParamUtil.isBlack(model.getCode())) {
            return new Resp(Resp.PARAM_ERROR, "请输入理财编号");
        }
        if (ParamUtil.isBlack(model.getAnnualEarnings())) {
            return new Resp(Resp.PARAM_ERROR, "预期年化不能为空");
        }
        if (ParamUtil.isBlack(model.getInvestmentDeadline())) {
            return new Resp(Resp.PARAM_ERROR, "请选择投资期限");
        }
        if (ParamUtil.isBlack(model.getInvestmentType())) {
            return new Resp(Resp.PARAM_ERROR, "请选择投资类型");
        }
        if (ParamUtil.isBlack(model.getEarningsType())) {
            return new Resp(Resp.PARAM_ERROR, "请选择收益类型");
        }
        if (ParamUtil.isBlack(model.getTotalPortion())) {
            return new Resp(Resp.PARAM_ERROR, "总份额不能为空");
        }
        if (ParamUtil.isBlack(model.getInterestDate())) {
            return new Resp(Resp.PARAM_ERROR, "起息日不能为空");
        }
        if (ParamUtil.isBlack(model.getDeadline())) {
            return new Resp(Resp.PARAM_ERROR, "期限不能为空");
        }
        if (null == model.getEarningsRate()) {
            return new Resp(Resp.PARAM_ERROR, "请填写收益率");
        }
        if (null == model.getEarningsRate()) {
            return new Resp(Resp.PARAM_ERROR, "收益率不能为空");
        }
        if (null == model.getMinInvestment()) {
            return new Resp(Resp.PARAM_ERROR, "请填写最小投资额");
        }
        if (model.getMinInvestment().compareTo(BigDecimal.ZERO) <= 0) {
            return new Resp(Resp.PARAM_ERROR, "最小投资额必须大于0");
        }
        if (null == model.getMaxInvestment()) {
            return new Resp(Resp.PARAM_ERROR, "请填写最大投资额");
        }
        if (model.getMaxInvestment().compareTo(BigDecimal.ZERO) <= 0) {
            return new Resp(Resp.PARAM_ERROR, "最大投资额必须大于0");
        }
        if (model.getMinInvestment().compareTo(model.getMaxInvestment()) == 1) {
            return new Resp(Resp.PARAM_ERROR, "最大投资额必须大于最小投资额");
        }
        if (ParamUtil.isBlack(model.getIsOnline())) {
            return new Resp(Resp.PARAM_ERROR, "请选择是否上线");
        }
        if (ParamUtil.isBlack(model.getTotalPortion())) {
            return new Resp(Resp.PARAM_ERROR, "总份额不能为空");
        }
        if (ParamUtil.isBlack(model.getDescription())) {
            return new Resp(Resp.PARAM_ERROR, "请输入理财说明");
        }
        if (ParamUtil.isBlack(model.getEnglishDescription())) {
            return new Resp(Resp.PARAM_ERROR, "请输入英文理财说明");
        }
        model.setIsExist(1);
        model.setStatus(1); //理财未开始
        model.setEndIntersetDate(FormatUtil.addDay(model.getInterestDate(), model.getDeadline()));//结息日为起息日+期限
        model.setLeftPortion(model.getTotalPortion());//剩余份额
        model.setCreateDate(new Date());
        model.setCreatePeople(currentStaff().getRealname());
        financialRepository.save(model);
        return new Resp("添加成功");
    }

    @PostMapping("/edit")
    public Resp edit(Financial model) {
        if (ParamUtil.isBlack(model.getFinancialId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财包");
        }
        Financial financial = financialRepository.findOne(model.getFinancialId());
        if (null == financial) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财包");
        }
        //判定该理财产品是否发生过购买行为
        List<UserFinancialRecord> userFinancialRecordList = userFinancialRecordRepository.findByFinancialId(model.getFinancialId());
        if (userFinancialRecordList.size() > 0) {
            return new Resp(Resp.PARAM_ERROR, "理财包已发生投资购买行为,不允许修改");
        }
        //如果当前购买时间大于起息日
        if (2 == financial.getStatus()) {
            return new Resp(Resp.PARAM_ERROR, "理财包已发生投资购买行为,不允许修改");
        }
        if (ParamUtil.isBlack(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "名称不能为空");
        }
        if (ParamUtil.isBlack(model.getEnglishName())) {
            return new Resp(Resp.PARAM_ERROR, "英文名称不能为空");
        }
        if (ParamUtil.isBlack(model.getTitle())) {
            return new Resp(Resp.PARAM_ERROR, "标题不能为空");
        }
        if (ParamUtil.isBlack(model.getCode())) {
            return new Resp(Resp.PARAM_ERROR, "请输入理财编号");
        }
        if (ParamUtil.isBlack(model.getAnnualEarnings())) {
            return new Resp(Resp.PARAM_ERROR, "预期年化不能为空");
        }
        if (ParamUtil.isBlack(model.getInvestmentDeadline())) {
            return new Resp(Resp.PARAM_ERROR, "请选择投资期限");
        }
        if (ParamUtil.isBlack(model.getInvestmentType())) {
            return new Resp(Resp.PARAM_ERROR, "请选择投资类型");
        }
        if (ParamUtil.isBlack(model.getEarningsType())) {
            return new Resp(Resp.PARAM_ERROR, "请选择收益类型");
        }
        if (ParamUtil.isBlack(model.getTotalPortion())) {
            return new Resp(Resp.PARAM_ERROR, "总份额不能为空");
        }
        if (ParamUtil.isBlack(model.getInterestDate())) {
            return new Resp(Resp.PARAM_ERROR, "起息日不能为空");
        }
        if (ParamUtil.isBlack(model.getDeadline())) {
            return new Resp(Resp.PARAM_ERROR, "期限不能为空");
        }
        if (null == model.getEarningsRate()) {
            return new Resp(Resp.PARAM_ERROR, "请填写收益率");
        }
        if (null == model.getEarningsRate()) {
            return new Resp(Resp.PARAM_ERROR, "收益率不能为空");
        }
        if (null == model.getMinInvestment()) {
            return new Resp(Resp.PARAM_ERROR, "请填写最小投资额");
        }
        if (model.getMinInvestment().compareTo(BigDecimal.ZERO) <= 0) {
            return new Resp(Resp.PARAM_ERROR, "最小投资额必须大于0");
        }
        if (null == model.getMaxInvestment()) {
            return new Resp(Resp.PARAM_ERROR, "请填写最大投资额");
        }
        if (model.getMaxInvestment().compareTo(BigDecimal.ZERO) <= 0) {
            return new Resp(Resp.PARAM_ERROR, "最大投资额必须大于0");
        }
        if (model.getMinInvestment().compareTo(model.getMaxInvestment()) == 1) {
            return new Resp(Resp.PARAM_ERROR, "最大投资额必须大于最小投资额");
        }
        if (ParamUtil.isBlack(model.getIsOnline())) {
            return new Resp(Resp.PARAM_ERROR, "请选择是否上线");
        }
        if (ParamUtil.isBlack(model.getTotalPortion())) {
            return new Resp(Resp.PARAM_ERROR, "总份额不能为空");
        }
        if (ParamUtil.isBlack(model.getDescription())) {
            return new Resp(Resp.PARAM_ERROR, "请输入理财说明");
        }
        if (ParamUtil.isBlack(model.getEnglishDescription())) {
            return new Resp(Resp.PARAM_ERROR, "请输入英文理财说明");
        }
        financial.setName(model.getName());
        financial.setTitle(model.getTitle());
        financial.setAnnualEarnings(model.getAnnualEarnings());
        financial.setInvestmentDeadline(model.getInvestmentDeadline());
        financial.setInvestmentType(model.getInvestmentType());
        financial.setEarningsType(model.getEarningsType());
        financial.setTotalPortion(model.getTotalPortion());
        financial.setLeftPortion(financial.getTotalPortion());
        financial.setInterestDate(model.getInterestDate());
        financial.setEndIntersetDate(FormatUtil.addDay(model.getInterestDate(), model.getDeadline()));//结息日为起息日+期限
        financial.setDeadline(model.getDeadline());
        financial.setEarningsRate(model.getEarningsRate());
        financial.setMinInvestment(model.getMinInvestment());
        financial.setMaxInvestment(model.getMaxInvestment());
        financial.setIsOnline(model.getIsOnline());
        financial.setDescription(model.getDescription());
        financial.setRemark(model.getRemark());
        financial.setCode(model.getCode());
        financial.setEnglishName(model.getEnglishName());
        financial.setEnglishDescription(model.getEnglishDescription());
        financialRepository.save(financial);
        return new Resp("修改成功");
    }

    @PostMapping("/remove")
    public Resp remove(Financial model) {
        if (ParamUtil.isBlack(model.getFinancialId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财包");
        }
        Financial financial = financialRepository.findOne(model.getFinancialId());
        if (null == financial) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财包");
        }
        //判定该理财产品是否发生过购买行为
        List<UserFinancialRecord> userFinancialRecordList = userFinancialRecordRepository.findByFinancialId(model.getFinancialId());
        if (userFinancialRecordList.size() > 0) {
            return new Resp(Resp.PARAM_ERROR, "理财包已发生投资购买行为,不允许修改");
        }
        //如果当前购买时间大于起息日
        if (2 == financial.getStatus()) {
            return new Resp(Resp.PARAM_ERROR, "理财包已发生投资购买行为,不允许修改");
        }
        financial.setIsExist(2); //设定状态为2,用于逻辑删除
        financialRepository.save(financial);
        return new Resp("删除成功");
    }


    /**
     * @author drj
     * @date 2018/8/5 14:48
     * @Description:修改理财包收益率
     */
    @PostMapping("/update_earnings_rate")
    public Resp updateEarningsRate(Long financialId, BigDecimal earningsRate) {
        if (ParamUtil.isBlack(financialId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财包");
        }
        Financial financial = financialRepository.findOne(financialId);
        if (null == financial) {
            return new Resp(Resp.PARAM_ERROR, "理财包不存在");
        }
        if (null == earningsRate) {
            return new Resp(Resp.PARAM_ERROR, "收益率不能为空");
        }
        financial.setEarningsRate(earningsRate);
        financialRepository.save(financial);
        return new Resp("修改成功");
    }
}
